#include "InputManager.h"
#include "../GameEngine.h"

InputManager* InputManager::_instance = NULL; // initialization

InputManager::InputManager() {
}

void InputManager::create()
{
	if (_instance == NULL)
	{
		_instance = new InputManager();
		
	}
}

void InputManager::destroy()
{
	delete _instance;
	_instance = NULL;

}

InputManager::~InputManager()
{
}

InputManager* InputManager::Instance() {
	return _instance;
}


void InputManager::update(WPARAM wParam, LPARAM	lParam, UINT uMsg) {

	switch (uMsg) {
		case WM_MOUSEMOVE: {	//A mouse is moved
			//Check the Game State and iterate over the objects that are listening
				POINT cursorPos;
				GetCursorPos(&cursorPos);
				std::list<MouseListener *>::iterator it;
				for(it=mouseListenersList.begin() ; it != mouseListenersList.end(); ++it) 
					(*it)->onMouseMoved(cursorPos.x, cursorPos.y);
				break;
		}
		case WM_LBUTTONDOWN: {	//A mouse is clicked
			//Check the Game State and iterate over the objects that are listening
				POINT cursorPos;
				GetCursorPos(&cursorPos);
				std::list<MouseListener *>::iterator it;
				for(it=mouseListenersList.begin() ; it != mouseListenersList.end(); ++it) 
					(*it)->onMouseClicked(cursorPos.x, cursorPos.y);
				break;
		}
		case WM_MOUSEWHEEL: {	//A mouse is scrolled
			//Check the Game State and iterate over the objects that are listening
				POINT cursorPos;
				GetCursorPos(&cursorPos);
				std::list<MouseListener *>::iterator it;
				for(it=mouseListenersList.begin() ; it != mouseListenersList.end(); ++it) 
					(*it)->onMouseScrolled(cursorPos.x, cursorPos.y, wParam);
				break;
		}
		case WM_KEYDOWN: {		//A key is pressed
			//First modify the Game State
			//GameEngine::Instance()->onKeyPressed(wParam);
			//Then check the Game State and iterate over the objects that are listening
			std::list<KeyboardListener *>::iterator it;
			for(it=keyboardListenersList.begin() ; it != keyboardListenersList.end(); ++it)
				(*it)->onKeyPressed(wParam);
			break;
		}
		case WM_KEYUP: {		//A key is released
			//Check the Game State and iterate over the objects that are listening
			std::list<KeyboardListener *>::iterator it;
			for(it=keyboardListenersList.begin() ; it != keyboardListenersList.end(); ++it) 
				(*it)->onKeyReleased(wParam);
			break;
		}

	}
}

void InputManager::registerKeyboardListener(KeyboardListener * keyListenerObject)
{
	keyboardListenersList.push_back(keyListenerObject);
}

void InputManager::deRegisterKeyboardListener(KeyboardListener * keyListenerObject)
{
	keyboardListenersList.remove(keyListenerObject);
}

void InputManager::registerMouseListener(MouseListener * mouseListenerObject)
{
	mouseListenersList.push_back(mouseListenerObject);
}

void InputManager::deRegisterMouseListener(MouseListener * mouseListenerObject)
{
	mouseListenersList.remove(mouseListenerObject);
}